package com.second.app.generic.test;


import com.second.app.enums.CommonCodeEnum;
import com.second.app.exception.MyException;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author GYZ
 * @Date 2023/10/13
 * <pre>
 *  定义泛型接口的子类
 * </pre>
 */
public class InfoImpl<T> implements Info<T> {

    private T var;

    public InfoImpl(T var) {
        this.setVar(var);
    }

    public void setVar(T var) {
        this.var = var;
    }

    @Override
    public T getVar() {
        return this.var;
    }

    /**
     * @param c
     * @param <T> 声明一个泛型T
     * @return <T>声明此方法持有一个类型T,也可以理解为声明此方法为泛型方法; T:方法的返回值为类型T
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public <T> T getObject(Class<T> c) throws InstantiationException, IllegalAccessException {
        return c.newInstance();
    }


    @SafeVarargs
    public static <T> T[] fun1(T... args) {
        return args;
    }



    public static <T> void fun2(T[] param) {
        System.out.println("接收泛型数组：");
        for (T t : param) {
            System.out.println(t + "、");
        }
    }
    /**
     *
     * @param param  泛型参数，并且指定参数是Number或者Number的子类实现
     * @return 返回 T
     */
    public static <T extends Number> T fun3(T param) {
        return param;
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Integer a = 10;

        Integer b =9;

        System.out.println(a.compareTo(b));
        System.out.println("=================================================");

        Integer val = fun3(300);
        System.out.println(val);
        System.out.println("=================================================");
        Integer[] iarr = fun1(1, 2, 3, 4, 5, 6);
        fun2(iarr);
        System.out.println("=================================================");
        InfoImpl<String> info = new InfoImpl<>("tom汤姆");
        System.out.println("内容： " + info.getVar());

        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(1);  //这样调用 add 方法只能存储整形，因为泛型类型的实例为 Integer

        list.getClass().getMethod("add", Object.class).invoke(list, "asd");

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
